package comu.hnevc.zh.testdemo.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.zip.Inflater;

import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.adapter.TrafficlightAdapter;
import comu.hnevc.zh.testdemo.base.BaseUrl;
import comu.hnevc.zh.testdemo.bean.Light;
import comu.hnevc.zh.testdemo.dialog.TrafficlightDialog;

public class TrafficlightManagerFragment extends Fragment {
    List<Light> mLightlist;
    private ListView listView;
    private Spinner sp;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.trafficlight_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView=getActivity().findViewById(R.id.lv_trafficlight);
        Button query=getActivity().findViewById(R.id.bt_query_traffic);
        Button allset=getActivity().findViewById(R.id.bt_traffic_allset);
        allset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrafficlightDialog dialog=new TrafficlightDialog(getContext(), mLightlist);
                dialog.show();

            }
        });
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initView();
            }
        });
        sp=getActivity().findViewById(R.id.sp_trafficlight);
        initdata();
    }

    private void initdata() {
        mLightlist=new ArrayList<>();
        for (int i=0;i<5;i++){
            SharedPreferences spf=getActivity().getSharedPreferences("data",Context.MODE_PRIVATE);
            String user=spf.getString("UserName","user2");
            final JSONObject object=new JSONObject();
            try {
                object.put("TrafficLightId",i+1);
                object.put("UserName",user);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            RequestQueue mQueue=Volley.newRequestQueue(getContext());
            JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.GetTrafficLightConfigAction, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Light light=new Light();
                    try {
                        light.setRoadnum(object.getInt("TrafficLightId"));
                        light.setRedtime(jsonObject.getInt("RedTime"));
                        light.setGreentime(jsonObject.getInt("GreenTime"));
                        light.setYellowtime(jsonObject.getInt("YellowTime"));
                        light.setNeedcharge(false);
                        mLightlist.add(light);
                        if (mLightlist.size()==5){
                            initView();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            mQueue.add(jor);
        }

    }

    private void initView() {
        if (sp.getSelectedItem().equals("路口升序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light light1, Light light2) {
                    return light1.getRoadnum()-light2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if(sp.getSelectedItem().equals("路口降序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if (sp.getSelectedItem().equals("红灯升序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if(sp.getSelectedItem().equals("红灯降序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if (sp.getSelectedItem().equals("黄灯升序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if (sp.getSelectedItem().equals("黄灯降序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if (sp.getSelectedItem().equals("绿灯升序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });

            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }else if (sp.getSelectedItem().equals("绿灯降序")){
            Collections.sort(mLightlist, new Comparator<Light>() {
                @Override
                public int compare(Light o1, Light o2) {
                    return o1.getRoadnum()-o2.getRoadnum();
                }
            });
            TrafficlightAdapter adapter=new TrafficlightAdapter(getContext(),mLightlist);
            listView.setAdapter(adapter);
        }
    }
}
